package Test_Web;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class SingleStar
 */
public class SingleStar extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SingleStar() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		
		// Get Parameters
		String starId = request.getParameter("id");
		
		try
		{
			try 
			{
				Class.forName("com.mysql.jdbc.Driver").newInstance();
			} 
			catch (Exception e) 
			{
				e.printStackTrace();
			}
		// Create Connection
		Connection connect;
		connect = DriverManager.getConnection("jdbc:mysql:///moviedb","testuser","testpass");
		Statement tempStatement = connect.createStatement();
		
		// Create and Execute Query
		String command = "select * from stars where id = " + starId;
		ResultSet resultSet = tempStatement.executeQuery(command);
		
		resultSet.next();
		String firstName = resultSet.getString("first_name");
		String lastName = resultSet.getString("last_name");
		String id = resultSet.getString("id");
		String dob = resultSet.getString("dob");
		String photo_url = resultSet.getString("photo_url");
		
		out.print("<!DOCTYPE html>");
		out.print("<html>");
		out.print("<body>");
		out.print("");
		out.print("<h1>" + firstName + " " + lastName+ "</h1>");
		out.print("");
		out.print("<p>Star ID = "+ id + "</p>");
		out.print("");
		out.print("<p>Date of Birth = "+ dob + "</p>");
		out.print("");
		out.print("<img src = '"+ photo_url +"'>");
		out.print("");
		
		// Display movies
		out.println("<p> Movies = ");
		String starQuery = "select movies.title, movies.id from movies, stars, stars_in_movies where movies.id = movie_id AND stars.id = star_id AND stars.id = " +
             id + " GROUP BY movies.title, movies.id";
		Statement tempStatement2 = connect.createStatement();
		ResultSet titleResultSet = tempStatement2.executeQuery(starQuery);
		
		while(titleResultSet.next())
		{
			String movieId = titleResultSet.getString("id");
			String title = titleResultSet.getString("title");
			out.println("<a href = 'SingleMovie?id="+ movieId +"'>" +
					title +"</a> | ");
		}
		out.println("</p>");
		// end display movies
		
		out.print("</body>");
		out.print("</html>");
		out.print("");
		}
		catch (SQLException ex) {
            while (ex != null) {
                  System.out.println ("SQL Exception:  " + ex.getMessage ());
                  ex = ex.getNextException ();
              }  // end while
          }  // end catch SQLException

		catch(java.lang.Exception ex)
          {
              out.println("<HTML>" +
                          "<HEAD><TITLE>" +
                          "MovieDB: Error" +
                          "</TITLE></HEAD>\n<BODY>" +
                          "<P>SQL error in doGet: " +
                          ex.getMessage() + "</P></BODY></HTML>");
              return;
          }
	}

}
